//
//  LEBuyVerifyView.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/20.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "LEBuyVerifyView.h"
@interface LEBuyVerifyView()
@property(nonatomic,strong) UILabel *titleLab;
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UILabel *hint1;
@property(nonatomic,strong) UILabel *hint2;
@property(nonatomic,strong) UITextField *countTextField;
@property(nonatomic,strong) UIButton *okBtn;
@property(nonatomic,strong) UIButton *cancelBtn;
@property(nonatomic,assign) BOOL isMove;
@end
@implementation LEBuyVerifyView

-(instancetype)init{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
        [self addGestureRecognizer:tap];
        [self.bgView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ViewTaped)]];
        
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.titleLab];
        [self.bgView addSubview:self.hint1];
        [self.bgView addSubview:self.hint2];
        [self.bgView addSubview:self.countTextField];
        [self.bgView addSubview:self.okBtn];
        [self.bgView addSubview:self.cancelBtn];
        self.titleLab.hidden = YES;
        self.hint1.hidden = YES;
        self.hint2.hidden = YES;
        self.countTextField.hidden = YES;
        self.okBtn.hidden = YES;
        self.cancelBtn.hidden = YES;
        [_bgView makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeMake(0, 0));
        }];
        [_titleLab makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(GGRealValue(5));
            make.centerX.equalTo(_bgView);
            make.size.equalTo(CGSizeMake(GGRealValue(60), GGRealValue(25)));
        }];
        [_hint1 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgView).offset(GGRealValue(10));
            make.centerY.equalTo(_bgView);
            //            make.right.equalTo(_countTextField.mas_left).offset(-GGRealValue(2));
        }];
        [_countTextField makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_hint1.mas_right).offset(GGRealValue(2));
            make.centerY.equalTo(_hint1);
            make.size.equalTo(CGSizeMake(GGRealValue(60), GGRealValue(20)));
        }];
        
        [_hint2 makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_countTextField);
            make.left.equalTo(_countTextField.mas_right).offset(GGRealValue(2));
        }];
        [_okBtn makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView).offset(-GGRealValue(10));
            make.left.equalTo(_bgView).offset(GGRealValue(20));
            make.size.equalTo(CGSizeMake(GGRealValue(50), GGRealValue(28)));
        }];
        [_cancelBtn makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView).offset(-GGRealValue(10));
            make.right.equalTo(_bgView).offset(-GGRealValue(20));
            make.size.equalTo(CGSizeMake(GGRealValue(50), GGRealValue(28)));
        }];
    }
    
    return self;
}

-(void)changeNextBtnLayout:(NSNotification *)notification{
    self.isMove = YES;
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        [_bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
            make.top.equalTo(self).offset(-GGRealValue(20));
        }];
        [self layoutIfNeeded];
    }];
}
- (void) reNextBtnLayout:(NSNotification *)notification{
    
    self.isMove = NO;
    [UIView animateWithDuration:0.3 animations:^{
        [_bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
        }];
        [self layoutIfNeeded];
    }];
}

- (void)ViewTaped{
    [self.bgView endEditing:YES];
}
-(void)showLEBuyVerifyViewWithType:(int)type Name:(NSString *)name andCount:(int)count{
    
    self.titleLab.text = type == 1?@"购买":@"出售";
    self.hint1.text = type == 1?@"你想购买":@"你想出售";
    
    self.hidden = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeNextBtnLayout:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reNextBtnLayout:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    self.hint2.text = [NSString stringWithFormat:@"个%@？",name];
    self.countTextField.text = [NSString stringWithFormat:@"%d",count];
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.48 initialSpringVelocity:0.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        [self.bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.titleLab.hidden = NO;
        self.hint1.hidden = NO;
        self.hint2.hidden = NO;
        self.countTextField.hidden = NO;
        self.okBtn.hidden = NO;
        self.cancelBtn.hidden = NO;
    }];
    
}

-(void)dismissView{
    if (self.isMove == YES) {
        [self ViewTaped];
    }else{
        [self.superview layoutIfNeeded];
        [UIView animateWithDuration:0.3 animations:^{
            self.titleLab.hidden = YES;
            self.hint1.hidden = YES;
            self.hint2.hidden = YES;
            self.countTextField.hidden = YES;
            self.okBtn.hidden = YES;
            self.cancelBtn.hidden = YES;
            [self.bgView remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self);
                make.size.equalTo(CGSizeZero);
            }];
            [self.superview layoutIfNeeded];//强制绘制
        }completion:^(BOOL finished) {
            self.hidden = YES;
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
            self.disBlock();
        }];
    }
}
-(void)okBtnClick{
    if (self.isMove == YES) {
        [self ViewTaped];
    }else{
       [self.delegate LEBuyVerifyViewBtnClickWithView:self Text:self.countTextField.text];
        [self dismissView];
    }
}
-(void)cancelBtnClick{
    
    [self dismissView];
}
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        [_bgView setBackgroundColor:HEXColor(0xffffff, 1)];
        _bgView.layer.borderColor = HEXColor(0x000000, 1).CGColor;
        _bgView.layer.borderWidth = 1;
        _bgView.layer.cornerRadius = 8;
    }
    return _bgView;
}
-(UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithTitle:@"购买" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(16)];
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}
-(UILabel *)hint1{
    if (!_hint1) {
        _hint1 = [[UILabel alloc] initWithTitle:@"你想购买" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(14)];
        _hint1.textAlignment = NSTextAlignmentRight;
    }
    return _hint1;
}

-(UILabel *)hint2{
    if (!_hint2) {
        _hint2 = [[UILabel alloc] initWithTitle:@"" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(14)];
        _hint2.textAlignment = NSTextAlignmentLeft;
    }
    return _hint2;
}

-(UITextField *)countTextField{
    if (!_countTextField) {
        _countTextField = [UITextField new];
        _countTextField.borderStyle = UITextBorderStyleLine;
        _countTextField.backgroundColor = [UIColor clearColor];
        _countTextField.textColor = HEXColor(0x000000, 1);
        _countTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _countTextField.clearButtonMode = UITextFieldViewModeNever;
        _countTextField.tintColor = HEXColor(0x000000, 1);
        _countTextField.font = [UIFont systemFontOfSize:GGRealValue(14)];
    }
    return _countTextField;
}

-(UIButton *)okBtn{
    if (!_okBtn) {
        _okBtn = [UIButton new];
        [_okBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_okBtn setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
        [_okBtn setTitle:@"好的" forState:UIControlStateNormal];
        [_okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}

-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton new];
        [_cancelBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_cancelBtn setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"算了" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
@end
